package moodish.client;

import java.awt.Dimension;
import java.util.LinkedList;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;

public class MoodsWindow {
	private LinkedList<String> moods;
	private JFrame frame;
	private JTable table;
	private TableModel model;

	public MoodsWindow(JFrame window, LinkedList<String> moods, String windowName) {
		super();
		this.moods = moods;
		frame = new JFrame(windowName);
		model = new TableModel();
		table = new JTable(model);
		frame.add(new JScrollPane(table));
		frame.setMinimumSize(new Dimension(300, 200));
		frame.setLocationRelativeTo(window);
		frame.setVisible(true);
	}

	/**
	 * Closes the window and releases all related resources.
	 */
	public void close() {
		frame.dispose();
	}

	/**
	 * 
	 * @return JFrame
	 */
	public JFrame getFrame() {
		return frame;
	}

	/**
	 * Gets the latest value for every table position
	 */
	public void update() {
		model.fireTableDataChanged();
	}

	/**
	 * Table Model for a single column table of Strings that gets each value
	 * from the LinkedList given when instantiated
	 */
	private class TableModel extends AbstractTableModel {

		@Override
		public int getColumnCount() {
			// TODO Auto-generated method stub
			return 1;
		}

		@Override
		public int getRowCount() {
			// TODO Auto-generated method stub
			return moods.size();
		}

		@Override
		public String getColumnName(int col) {
			switch (col) {
			case 0:
				return "Mood";
			default:
				return null;
			}
		}

		@Override
		public Object getValueAt(int row, int col) {
			return moods.get(row);

		}
	}
}